<?php
namespace common\components;  
use Yii;  
use yii\base\Component;  
use yii\base\InvalidConfigException; 

class HelpComponent extends Component{
	
        public static function truncate_utf8_string($string, $length, $etc = '...')
        {
            $result = '';
            $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
            $strlen = strlen($string);
            for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
                {
                if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
                        {
                    if ($length < 1.0)
                                {
                        break;
                    }
                    $result .= substr($string, $i, $number);
                    $length -= 1.0;
                    $i += $number - 1;
                }
                        else
                        {
                    $result .= substr($string, $i, 1);
                    $length -= 0.5;
                }
            }
            $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
            if ($i < $strlen)
                {
                        $result .= $etc;
            }
            return $result;
        }
        
        public static function make_decode($str){
            return unserialize(base64_decode($str));
        }
        
        public static function make_encode($data){
            return base64_encode(serialize($data));
        }
        
 
        public static function getFirstZhChar($str) 
        { 
            $str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的，此行就要注释掉 
            if (preg_match("/^[\x7f-\xff]/", $str)) 
            { 
                $fchar=ord($str{0}); 
                if($fchar>=ord("A") and $fchar<=ord("z") )return strtoupper($str{0}); 
                $a = $str; 
                $val=ord($a{0})*256+ord($a{1})-65536; 
                if($val>=-20319 and $val<=-20284)return "A"; 
                if($val>=-20283 and $val<=-19776)return "B"; 
                if($val>=-19775 and $val<=-19219)return "C"; 
                if($val>=-19218 and $val<=-18711)return "D"; 
                if($val>=-18710 and $val<=-18527)return "E"; 
                if($val>=-18526 and $val<=-18240)return "F"; 
                if($val>=-18239 and $val<=-17923)return "G"; 
                if($val>=-17922 and $val<=-17418)return "H"; 
                if($val>=-17417 and $val<=-16475)return "J"; 
                if($val>=-16474 and $val<=-16213)return "K"; 
                if($val>=-16212 and $val<=-15641)return "L"; 
                if($val>=-15640 and $val<=-15166)return "M"; 
                if($val>=-15165 and $val<=-14923)return "N"; 
                if($val>=-14922 and $val<=-14915)return "O"; 
                if($val>=-14914 and $val<=-14631)return "P"; 
                if($val>=-14630 and $val<=-14150)return "Q"; 
                if($val>=-14149 and $val<=-14091)return "R"; 
                if($val>=-14090 and $val<=-13319)return "S"; 
                if($val>=-13318 and $val<=-12839)return "T"; 
                if($val>=-12838 and $val<=-12557)return "W"; 
                if($val>=-12556 and $val<=-11848)return "X"; 
                if($val>=-11847 and $val<=-11056)return "Y"; 
                if($val>=-11055 and $val<=-10247)return "Z"; 
            }  
            else 
            { 
                return false; 
            } 
        }       

        public static function filterNoUtf8($str){
                $result = preg_replace('/[\x{10000}-\x{10FFFF}]/u','',$str);
                return $result;
        }
}


?>